package huawei.exam;

import java.util.Scanner;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/7/1
 */
public class MaxCost {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] split = s.split(",");
        int[] price = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            price[i] = Integer.parseInt(split[i]);
        }
        int money = Integer.parseInt(scanner.nextLine());
        int maxCost = getMaxCost(price, money, 3);
        System.out.println(maxCost);
    }

    private static int getMaxCost(int []cost,int money,int v) {
        int maxCost = -1;
        int[][] dp = new int[cost.length + 1][v + 1];
        for (int i = 1; i <= cost.length; i++) {
            for(int j = 1;j <= v;j++) {
                dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-1] + cost[i -1]);
            }
        }

        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < dp[i].length; j++) {
                System.out.print(dp[i][j] + " ");
            }
            System.out.println();
        }

        if (dp[cost.length][v] <= money) {
            maxCost = dp[cost.length - 1][v];
        }
        return maxCost;
    }
}
